from rest_framework import serializers
from accounts.models import *


# 用户
class AccountsSerializers(serializers.ModelSerializer):
    children = serializers.SerializerMethodField(read_only=True)
    dept_yue = serializers.ReadOnlyField()
    date_joined = serializers.ReadOnlyField()

    def get_children(self, obj):
        user = self.context["request"].user
        user_set = Consumer.objects.filter(is_delete=False, parent=obj)
        if user.parent:
            user_set = user_set.filter(id=user.id)
        if user_set:
            serializer = AccountsSerializers(
                user_set, many=True, context=self.context
            ).data
        else:
            serializer = []
        return serializer

    class Meta:
        model = Consumer
        fields = "__all__"


class PermissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission
        fields = [
            "id",
            "code",
            "parent",
            "intro",
            "category",
            "status",
            "modifier",
            "creator",
        ]


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Role
        fields = "__all__"

class configSerializer(serializers.ModelSerializer):
    class Meta:
        model = config
        fields = "__all__"

class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = "__all__"


class ConsumerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Consumer
        fields = "__all__"


# 菜单
class MenuSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = StorageMenu
        fields = "__all__"

    def get_children(self, obj):
        menus = StorageMenu.objects.filter(status=True, parentPath=obj).order_by(
            "order"
        )
        serializer = MenuSerializer(menus, many=True, context=self.context)
        return serializer.data


class VideoFileAddresSerializer(serializers.ModelSerializer):
    class Meta:
        model = VideoFileAddres
        fields = "__all__"